using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

namespace visualtwimilar
{
	public class MainViewModel : INotifyPropertyChanged
	{
		private readonly List<UserShape> _users = new List<UserShape>();
		private DataInitialyzer _dataInitialyzer;

		public void Initiallize(string name, PropertyChangedEventHandler handler, double width, double height)
		{
			PropertyChanged += handler;
			_dataInitialyzer = new DataInitialyzer(this, name, width, height);
			_dataInitialyzer.Initialyze();
		}

		public List<UserShape> Users
		{
			get
			{
				return _users;
			}
		}

		#region onPropertyChanged

		private void OnPropertyChanged(string name)
		{
			if (PropertyChanged != null)
				PropertyChanged.Invoke(this, new PropertyChangedEventArgs(name));
		}

		public event PropertyChangedEventHandler PropertyChanged;

		#endregion

		public void ClearUsers()
		{
			_users.Clear();
			OnPropertyChanged("Users");
		}

		public void AddUser(string userName, double left, double top)
		{
			UserShape userShape;
			if (_users.Where(x => x.Name == userName).ToList().Count == 0)
			{
				userShape = new UserShape(userName);
				_users.Add(userShape);
				OnPropertyChanged("Users");
			}
			else
			{
				userShape = _users.Where(x => x.Name == userName).First();
			}
			userShape.Left = left;
			userShape.Top = top;
		}
	}
}